const MyQuery = require('../utils/db')
const path = require('path');

var express = require('express');
var router = express.Router();



//引入配置文件
const { API_URL } = require('../utils/config')

router.get('/demo1', (req, res) => {
    res.send('首页模块');
});

router.get('/', (req, res) => {
    res.send(path.join(__dirname, 'index.html'));
});

/* GET home page. */
router.get('/banners', async function(req, res, next) {
    let sql2 = `SELECT *,CONCAT('${API_URL}/uploads/banners/',image_src) AS image_src FROM e_banners WHERE isshow=1`
    let { err: err2, result: result } = await MyQuery(sql2);
    let data = {};
    if (result.length > 0) {
        data = {
            "msg": "OK",
            "status": 200,
            "result": result
        };
        res.send(data);
    }
});



router.get('/courseinfo', async(req, res, next) => {

    let { type = 1 } = req.query
    let size = type == 1 ? 8 : 6

    let data = {} //最终要返回的数据格式  msg,status,result
        //1. 写sql
    let sql = ` SELECT id,cid,title,price,CONCAT('${API_URL}/uploads/courses/',image_src) AS image_src,type,type_name,area_name,subject_name FROM e_course WHERE type= ${type} LIMIT ${size} `
        //2. 执行sql
    let { result } = await MyQuery(sql) //{ err,result }

    //3. 要处理成规范、格式化的json数据
    data = {
        msg: 'OK',
        status: 200,
        result
    }
    res.send(data)
})


module.exports = router;